<!-- case 详情 -->
<template>
    <div class="caseView" v-loading="loading">
        <dy-form :elForm_Options="elForm_Options" @bottonHandle="opbuttonclick" @openBack="openFunction"
            :flagIndex="flagIndex" ref="myForm" />
    </div>
</template>

<script setup>
import { ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import { Session } from '@/utils/storage'
import { reactive, toRefs } from 'vue'

const router = useRouter()
const route = useRoute()

let props = defineProps({
    flagIndex: {
        type: String,
        default: () => 'detail'
    }
})

let state = reactive({
    loading: false,
    currentRow: null
})
let { loading } = toRefs(state)

//表单数据
let elForm_Options = reactive({
    pagetitle: 'case包', //页面名称
    getwayname: "casepackage",//获取方法
    //操作button
    opbutton: [{
        label: "保存",
        dealway: "submitForm",
        icon: 'Checked',
        type: "primary",
        flagIndexList: ['edit', 'create'],
    }, {
        label: "编辑",
        dealway: "edit_btn",
        icon: 'Edit',
        flagIndexList: ['detail']
    },
    {
        label: "删除",
        dealway: "delete_btn",
        icon: 'Delete',
        type: "danger",
        plain: true,
        flagIndexList: ['detail']
    }],
    //表单循环
    wt_mesage: [{
        label: "标准试验包名称",
        prop: "standard_task_name",
        paramtype: 'input',
        required: true,
        allwidth: true
    }, {
        label: "所属部门",
        prop: "sy_owndeptid",
        paramtype: 'choosedepartment',
        required: true
    }, {
        label: "创建人",
        prop: "creater",
        required: true,
        paramtype: 'chooseuser'
    }, {
        label: "总需求占用台时",
        prop: "all_occupy_hour",
        paramtype: 'input'
    },
    {
        label: "试验描述",
        prop: "description",
        paramtype: 'input'
    }]
})

//实例
let myForm = ref(null)

onMounted(() => {
    if (props.flagIndex == 'create') {
        openFunction({})
    } else {
        myForm.value.getFormData()
    }
})

//打开页面执行
let openFunction = async (row) => {
    state.currentRow = JSON.parse(JSON.stringify(row))
    if (props.flagIndex == 'create') {
        state.currentRow.creater = [Session.get('UserMsg')]
    }
    //表单数据赋值
    myForm.value.setFormData(state.currentRow)
}

//按钮事件
let opbuttonclick = (wayname) => {
    if (wayname == 'submitForm') {
        submitForm()
        return
    }

    if (wayname == 'returnPage') {
        returnPage()
        return
    }

    if (wayname == 'edit_btn') {
        edit_btn()
        return
    }

    if (wayname == 'delete_btn') {
        delete_btn()
        return
    }
}

//返回
let returnPage = () => {
    //删除tagViews
    myForm.value.closeCurrentTagsView()
    if (['detail', 'create'].includes(props.flagIndex)) {
        //返回主页
        router.push({
            path: '/syStandardLib/casepackage'
        })
        return
    }
    router.push({
        path: '/syStandardLib/casepackage/view/' + route.params.id
    })
}

//编辑
let edit_btn = () => {
    router.push({
        path: '/syStandardLib/casepackage/edit/' + route.params.id
    })
}

//删除
let delete_btn = () => {
    ElMessageBox.confirm(
        '确定删除此条项目?',
        '提示',
        {
            confirmButtonText: '确 定',
            cancelButtonText: '取 消',
            type: 'warning',
        }
    ).then(async () => {
        state.loading = true
        await api.changeaxiosdata(elForm_Options.getwayname, 'delete', { id: route.params.id })
        ElMessage({
            message: '删除成功',
            type: 'success'
        })
        state.loading = false
        returnPage()
    }).catch(() => { })
}

//保存
let submitForm = () => {
    myForm.value.submitFormData()
}
</script>

<style lang="scss" scoped></style>